package com.cjkj.system.controller;

import com.cjkj.common.core.constant.EquipmentConstants;
import com.cjkj.common.core.constant.WarnConstants;
import com.cjkj.common.core.web.domain.AjaxResult;
import com.cjkj.common.security.utils.SecurityUtils;
import com.cjkj.system.api.domain.APIWarnHandle;
import com.cjkj.system.api.domain.THospitalUser;
import com.cjkj.system.api.domain.request.WarnRecordReq;
import com.cjkj.system.api.model.LoginUser;
import com.cjkj.system.domain.dto.PatWarnRecordReq;
import com.cjkj.system.domain.vo.ScreenPatientInfo;
import com.cjkj.system.service.*;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/screen")
public class MonitorScreenController {

    @Resource
    private ITPatVisitService visitService;

    @Resource
    private WarnService warnService;

    @Resource
    private ReportTJ01Service reportTJ01Service;

    @Resource
    private ReportTJ02Service reportTJ02Service;

    @Resource
    private ReportTJ03Service reportTJ03Service;

    /**
     * 查询患者基本信息
     **/
    @GetMapping("/listPat")
    public AjaxResult listPatientInfo(String keyWord, String dataType){
        LoginUser loginUser = SecurityUtils.getLoginUser();
        List<ScreenPatientInfo> patientInfoList = visitService.listScreenPatient(loginUser.getHospitalId(), loginUser.getDeptId(), keyWord, dataType);
        return AjaxResult.success(patientInfoList);
    }

    /**
     * 获取患者的体征信息
     **/
    @GetMapping("/patSign")
    public AjaxResult getPatientSignInfo(Long visitNo, String dataType
            , @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime
            , @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") Date endTime){
        if(EquipmentConstants.EQUIPMENT_TYPE_MONITOR_PAD_TYPE.equals(dataType)) {
            return AjaxResult.success(reportTJ01Service.getPatSign(visitNo, startTime, endTime));
        }
        if(EquipmentConstants.EQUIPMENT_TYPE_SMART_BRACELET_TYPE.equals(dataType)) {
            return AjaxResult.success(reportTJ02Service.getPatBraceletSign(visitNo));
        }
        if(EquipmentConstants.EQUIPMENT_TYPE_EB_TYPE.equals(dataType)) {
            return AjaxResult.success(reportTJ03Service.getPatTJ03Date(visitNo));
        }
        return AjaxResult.error("参数错误！");
    }

    /**
     * 预警列表
     * @param dataType
     * @return
     */
    @GetMapping("/warnList")
    public AjaxResult listWarn(String dataType){
        THospitalUser sysUser = SecurityUtils.getLoginUser().getSysUser();
        return AjaxResult.success(warnService.listWarn(sysUser.getHospitalId(), sysUser.getDeptId(), WarnConstants.WARN_HANDLE_UNPROCESSED, dataType));
    }

    /**
     * 患者预警消息
     * @param visitNo
     * @return
     */
    @GetMapping("/warnList/patient")
    public AjaxResult listWarn(Long visitNo){
        return AjaxResult.success(warnService.listWarn(visitNo));
    }

    @PostMapping("/warn/handle")
    public AjaxResult handleWarn(@RequestBody List<PatWarnRecordReq> recordReqList){
        THospitalUser sysUser = SecurityUtils.getLoginUser().getSysUser();
        List<APIWarnHandle> apiWarnHandleList = new ArrayList<>();
        recordReqList.forEach( e -> {
            APIWarnHandle apiWarnHandle = new APIWarnHandle();
            apiWarnHandle.setHospitalId(sysUser.getHospitalId());
            apiWarnHandle.setDeptId(sysUser.getDeptId());
            apiWarnHandle.setHandlerId(sysUser.getUserId());
            apiWarnHandle.setWarnId(e.getWarnId());
            apiWarnHandle.setWarnType(e.getWarnType());
            apiWarnHandle.setHandleWay(e.getHandleWay());
            apiWarnHandleList.add(apiWarnHandle);
        });
        Boolean handleFlag = warnService.handleWarn(apiWarnHandleList);
        if(handleFlag){
            return AjaxResult.success("处理成功");
        }else {
            return AjaxResult.error("处理失败");
        }
    }

    @PutMapping("/warn/handle/patient")
    public AjaxResult handleWarn(Long visitNo, String handleWay){
        THospitalUser sysUser = SecurityUtils.getLoginUser().getSysUser();
        WarnRecordReq req = new WarnRecordReq();
        req.setHospitalId(sysUser.getHospitalId());
        req.setDeptId(sysUser.getDeptId());
        req.setHandlerId(sysUser.getUserId());
        req.setVisitNo(visitNo);
        req.setHandleWay(handleWay);
        Boolean handleFlag = warnService.handleWarn(req);
        if(handleFlag){
            return AjaxResult.success("处理成功");
        }else {
            return AjaxResult.error("处理失败");
        }
    }

}
